package com.binfenhulian.chinaenergy.ces.manage.service.crawler;

import com.binfenhulian.chinaenergy.ces.core.exception.BaseException;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.CrawlerParam;
import com.binfenhulian.chinaenergy.ces.manage.core.db.dao.auto.CrawlerConfigDAO;
import com.binfenhulian.chinaenergy.ces.manage.core.db.dao.def.CrawlerConfigDefDAO;
import com.binfenhulian.chinaenergy.ces.manage.core.db.enums.CrawlerConfigStatusEnum;
import com.binfenhulian.chinaenergy.ces.manage.core.db.po.auto.CrawlerConfigPO;
import com.binfenhulian.chinaenergy.ces.utils.date.UnixTimestampUtils;
import com.binfenhulian.chinaenergy.ces.utils.uuid.UuidUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by wanghjbuf on 2018/3/14.
 */
@Service("com.binfenhulian.chinaenergy.ces.manage.service.crawler.CrawlerServiceImpl")
public class CrawlerServiceImpl implements CrawlerService{

    @Autowired
    CrawlerConfigDefDAO crawlerConfigDefDAO;
    @Autowired
    CrawlerConfigDAO crawlerConfigDAO;

    @Override
    public PageInfo listCrawlerByPage(Map<String,Object> paramMap) throws BaseException{
        try {
            Page page = PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
            PageInfo pageInfo = new PageInfo(crawlerConfigDefDAO.query(paramMap));
            return pageInfo;
        } catch (Exception e) {
            throw new BaseException("系统未知错误,操作失败!", e);
        }
    }

    @Transactional
    @Override
    public int save(CrawlerParam crawlerParam) throws BaseException{
        int result = 0;
        try {
            CrawlerConfigPO crawlerConfigPO = new CrawlerConfigPO();
            BeanUtils.copyProperties(crawlerParam, crawlerConfigPO);

            if (crawlerParam.getId() != null) {
                crawlerConfigPO.setUpdateTime(UnixTimestampUtils.currentTimes());
                result = crawlerConfigDAO.updateByPrimaryKeySelective(crawlerConfigPO);
            } else {
                crawlerConfigPO.setNo(UuidUtils.getUUID());
                crawlerConfigPO.setStatus(CrawlerConfigStatusEnum.INIT.name());
                crawlerConfigPO.setCreateTime(UnixTimestampUtils.currentTimes());
                crawlerConfigPO.setUpdateTime(UnixTimestampUtils.currentTimes());
                result = crawlerConfigDAO.insertSelective(crawlerConfigPO);
            }
        } catch (Exception e) {
            throw new BaseException("系统未知错误,操作失败!", e);
        }
        return result;
    }

    @Transactional
    @Override
    public int start(long id) throws BaseException {
        try {
            CrawlerConfigPO crawlerConfigPO = new CrawlerConfigPO();
            crawlerConfigPO.setId(id);
            crawlerConfigPO.setStatus(CrawlerConfigStatusEnum.RUNABLE.name());

            return crawlerConfigDAO.updateByPrimaryKeySelective(crawlerConfigPO);
        } catch (Exception e) {
            throw new BaseException("系统未知错误,操作失败!", e);
        }
    }

    @Transactional
    @Override
    public int stop(long id) throws BaseException {
        try {
            CrawlerConfigPO crawlerConfigPO = new CrawlerConfigPO();
            crawlerConfigPO.setId(id);
            crawlerConfigPO.setStatus(CrawlerConfigStatusEnum.INIT.name());

            return crawlerConfigDAO.updateByPrimaryKeySelective(crawlerConfigPO);
        } catch (Exception e) {
            throw new BaseException("系统未知错误,操作失败!", e);
        }
    }
}
